package com.neusoft.work.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.work.dao.BedInfoDao;
import com.neusoft.work.dao.RoomInfoDao;
import com.neusoft.work.dto.BedDTO;
import com.neusoft.work.dto.BedViewDTO;
import com.neusoft.work.po.BedInfo;
import com.neusoft.work.po.ResponseBean;
import com.neusoft.work.po.RoomInfo;
import com.neusoft.work.service.RoomInfoService;

@Service
public class RoomInfoServiceImpl implements RoomInfoService {
    @Autowired
    private RoomInfoDao roomInfoDao;
    
    @Autowired
    private BedInfoDao bedInfoDao;

    @Override
    public ResponseBean<List<String>> queryRoomNumberByFloor(Integer floor) {
        try {
            List<String> list = roomInfoDao.selectRoomNumberByFloor(floor);
            if (list != null && !list.isEmpty()) {
                return new ResponseBean<>(list);
            } else {
                return new ResponseBean<>(500, "该楼层无房间");
            }
        } catch (Exception e) {
            return new ResponseBean<>(500, "查询失败: " + e.getMessage());
        }
    }

    @Override
    public ResponseBean<Integer> queryUsageTypeByRoomNumber(String roomNumber) {
        try {
            Integer usageType = roomInfoDao.selectUsageTypeByRoomNumber(roomNumber);
            if (usageType != null) {
                return new ResponseBean<>(usageType);
            } else {
                return new ResponseBean<>(500, "未找到房间信息");
            }
        } catch (Exception e) {
            return new ResponseBean<>(500, "查询失败: " + e.getMessage());
        }
    }

	@Override
	public ResponseBean<List<String>> queryAllRoomNumber() {
		ResponseBean<List<String>> rb=null;
		List<String> list=roomInfoDao.selectRoomNumber();
		if(list!=null) {
			rb=new ResponseBean<>(list);
		}else {
			rb=new ResponseBean<>(500,"无房间");
		}
		return rb;
	}
	
	@Override
	public ResponseBean<List<Integer>> queryAllFloors() {
		try {
			List<RoomInfo> allRooms = roomInfoDao.selectList(null);
			List<Integer> floors = allRooms.stream()
					.map(RoomInfo::getFloor)
					.distinct()
					.sorted()
					.collect(Collectors.toList());
			return new ResponseBean<>(floors);
		} catch (Exception e) {
			return new ResponseBean<>(500, "查询楼层失败: " + e.getMessage());
		}
	}
	
	@Override
	public ResponseBean<List<BedViewDTO>> getRoomsByFloor(Integer floor) {
		try {
			List<RoomInfo> rooms = roomInfoDao.selectRoomsByFloor(floor);
			List<BedViewDTO> result = new ArrayList<>();
			
			for (RoomInfo room : rooms) {
				BedViewDTO bedViewDTO = new BedViewDTO();
				bedViewDTO.setId(room.getId());
				bedViewDTO.setFloor(room.getFloor());
				bedViewDTO.setRoomNumber(room.getRoomNumber());
				
				if (room.isWard()) {
					// 普通病房
					bedViewDTO.setType("ward");
					bedViewDTO.setName("普通房间");
					
					// 获取该房间的所有床位
					List<BedInfo> beds = bedInfoDao.selectBedsByRoomNumber(room.getRoomNumber());
					List<BedDTO> bedDTOs = new ArrayList<>();
					
					for (BedInfo bed : beds) {
						BedDTO bedDTO = new BedDTO();
						bedDTO.setId(bed.getId());
						bedDTO.setBedNumber(bed.getBedNumber());
						// 转换状态：1=available, 2=occupied, 3=outing
						switch (bed.getStatus()) {
							case 1:
								bedDTO.setStatus("available");
								break;
							case 2:
								bedDTO.setStatus("occupied");
								break;
							case 3:
								bedDTO.setStatus("outing");
								break;
							default:
								bedDTO.setStatus("available");
						}
						bedDTOs.add(bedDTO);
					}
					bedViewDTO.setBeds(bedDTOs);
				} else {
					// 功能房间
					bedViewDTO.setType("utility");
					bedViewDTO.setName(room.getRoomTypeName());
					bedViewDTO.setBeds(new ArrayList<>());
				}
				
				result.add(bedViewDTO);
			}
			
			return new ResponseBean<>(result);
		} catch (Exception e) {
			return new ResponseBean<>(500, "查询失败: " + e.getMessage());
		}
	}
	
	@Override
	public ResponseBean<Map<String, Integer>> getFloorStats(Integer floor) {
		try {
			Map<String, Integer> stats = new HashMap<>();

			List<BedInfo> bedsOnFloor = bedInfoDao.selectBedsByFloor(floor);

			long total = bedsOnFloor.size();
			long available = bedsOnFloor.stream().filter(b -> b.getStatus() == 1).count();
			long occupied = bedsOnFloor.stream().filter(b -> b.getStatus() == 2).count();
			long outing = bedsOnFloor.stream().filter(b -> b.getStatus() == 3).count();

			stats.put("total", (int) total);
			stats.put("available", (int) available);
			stats.put("occupied", (int) occupied);
			stats.put("outing", (int) outing);
			
			return new ResponseBean<>(stats);
		} catch (Exception e) {
			return new ResponseBean<>(500, "查询统计失败: " + e.getMessage());
		}
	}
}
